package com.cdream.basic.algorithm.leetcode.design;

import java.util.Comparator;
import java.util.Objects;
import java.util.TreeSet;

/**
 * @author cdream
 * @date 2022/1/23
 */

class Stock {
    public int price;
    public int timestamp;

    public Stock(int price, int timestamp) {
        this.price = price;
        this.timestamp = timestamp;
    }

    @Override
    public int hashCode() {
        return Objects.hash(timestamp);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Stock)) return false;
        Stock stock = (Stock) o;
        return timestamp == stock.timestamp;
    }
}

public class StockPrice {
    int[] currentPrice = null;
    TreeSet<Stock> set = new TreeSet<>(Comparator.comparingInt(a -> a.price));

    public StockPrice() {

    }

    public static void main(String[] args) {
        StockPrice stockPrice = new StockPrice();
        stockPrice.update(1, 10);
        stockPrice.update(2, 10);
        Stock stock1 = new Stock(5, 2);
        Stock stock2 = new Stock(5, 1);
        System.out.println(stockPrice);
    }

    public void update(int timestamp, int price) {
        if (currentPrice == null || currentPrice[0] <= timestamp) {
            currentPrice = new int[]{timestamp, price};
        }
        Stock s = new Stock(price, timestamp);
        set.remove(s);
        set.add(s);
    }

    public int current() {
        return currentPrice[1];
    }

    public int maximum() {
        return set.last().price;
    }

    public int minimum() {
        return set.first().price;
    }


}
